package com.hp.ssm.anno.config;


import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.stat.DruidDataSourceStatManager;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.*;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import java.io.IOException;


@Configuration
@EnableAspectJAutoProxy
@ComponentScan("com.hp.ssm.anno.service")
@PropertySource("classpath:application.properties")
@MapperScan("com.hp.ssm.anno.dao")
@EnableTransactionManagement
public class SpringMybatis {

    @Autowired
    private Environment environment;

    @Autowired
    private DruidDataSource druidDataSource;

    @Bean
    public DruidDataSource dataSource(){
        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setDriverClassName(environment.getProperty("jdbc.driverClass"));
        druidDataSource.setUrl(environment.getProperty("jdbc.connectionURL"));
        druidDataSource.setUsername(environment.getProperty("jdbc.username"));
        druidDataSource.setPassword(environment.getProperty("jdbc.password"));
        druidDataSource.setInitialSize(Integer.parseInt(environment.getProperty("jdbc.pool.init")));
        druidDataSource.setMinIdle(Integer.parseInt(environment.getProperty("jdbc.pool.minIdle")));
        druidDataSource.setMaxActive(Integer.parseInt(environment.getProperty("jdbc.pool.maxActive")));
        return druidDataSource;
    }

    @Bean
    public SqlSessionFactoryBean sessionFactoryBean() throws IOException {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(druidDataSource);
        sqlSessionFactoryBean.setTypeAliasesPackage("com.hp.ssm.anno");

        ClassPathResource applicationContext = new ClassPathResource("mybatis-config.xml");
        sqlSessionFactoryBean.setConfigLocation(applicationContext);

        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        sqlSessionFactoryBean.setMapperLocations(resolver.getResources("classpath:/mapper/**/*.xml"));
        return sqlSessionFactoryBean;
    }

    @Bean
    public PlatformTransactionManager transactionManager(){
        DataSourceTransactionManager ds = new DataSourceTransactionManager();
        ds.setDataSource(druidDataSource);
        return ds;
    }

    //配置 JdbcTemplate 数据持久层模板
    @Bean
    public JdbcTemplate jdbcTemplate(){
        JdbcTemplate jdbcTemplate = new JdbcTemplate();
        jdbcTemplate.setDataSource(druidDataSource);
        return jdbcTemplate;
    }

}
